<?php
class XUdpd 
{
	private $m_ip;
	private $m_gateway;
	private $m_mac;
	private $m_udpPort;
	
	public function getIp() { return $this->m_ip; } 
	public function getGateway() {return $this->m_gateway;}
	public function getUdpPort() {return $this->m_udpPort;}
	public function getMac() {return $this->m_mac;}
	public function setIp($ip) { $this->m_ip = $ip; }
    public function setGateway($gateway) { $this->m_gateway = $gateway;}
	//public function setUdpPort($port) {$this->m_udpPort = $port ;} 

	public function setMac($mac) {
		$pwd="/sbin/ifconfig eth0 hw ether fe:ed:ac:1d:be:ef";
		exec($pwd);
		$this->m_mac = $mac;
		$macCmd = "/sbin/ifconfig eth0 hw ether ".$mac;
		exec($macCmd);
	}
	
	function __construct()
	{
		$this->readUdpd();
	}
	
	public function readUdpd()
	{
		$file ="/usr/infomat/udpd.conf";
		if (false === ($fd = fopen($file, 'r'))) {
			die("[NG] Open udpd.conf\r");
            $this->m_ip = $this->m_gateway = $this->m_udpPort = $this->m_mac = "[NG] Cannot Find Configure File";
            return;
		} 
		$tmpIp = trim(fgets($fd));
		$this->m_ip = str_replace("IPV4ADDRESS=","", $tmpIp);
		
		$tmpGateway = trim(fgets($fd));	
		$this->m_gateway = str_replace("GATEWAY=","", $tmpGateway);
		
		$tmpUdpPort = trim(fgets($fd));
		//$this->m_udpPort = str_replace("UDPPORT=","", $tmpUdpPort);
		$this->m_udpPort = "5042";
		
		$nothing = fgets($fd);
		$nothing = fgets($fd);
		
		//comment out: Mac Address is not from  udpd.conf
		//$tmpMac = trim(fgets($fd));
		//$this->m_mac = str_replace("MACADDR=","", $tmpMac);
        $macExp = "/sbin/ifconfig eth0 | sed -n '/HWaddr/ s/^.*HWaddr *//pg'";
		//$macExp = "bash -c \"echo aaa\"";
		$this->m_mac = exec($macExp);

        if (empty($this->m_ip)) {
            $this->m_ip = "[NG] invalid data";
        }
        
        if (empty($this->m_gateway)) {
            $this->m_gateway = "[NG]Get invalid gateway";
        }
        
        if (empty($this->m_udpPort)) {
            $this->m_udpPort = "[NG]Get invalid UDP Port";
        }
         
        if (empty($this->m_mac)) {
            $this->m_mac = "[NG]Get invalid mac address";
        }
		
		fclose($fd);
	}
	
	public function writeUdpd() 
	{
		$file ="/usr/infomat/udpd.conf";
		$fileBak ="/usr/infomat/udpd.conf.bak";
		if (rename($file, $fileBak) === false) {
			echo "[NG] Backup Failed <br/>" ;
			return;
		}
		
		if (($fd = fopen($file, 'w')) === false) {
			echo "[NG] Cannot write udpd.conf Failed<br/>";
			return;
		}
		
		if (is_writeable($file) === false) {
			echo "[NG] Not Writable file <br/>"; 
			return;
		}
		
		$ip = "IPV4ADDRESS=".$this->m_ip."\n";
		$gateway = "GATEWAY=".$this->m_gateway."\n";
		//$udpport = "UDPPORT=".$this->m_udpPort."\n";
		$udpport = "UDPPORT=5042"."\n";
		$timeout = "TIMEOUT=0\n";
		$protocol = "PROTOCOL=UDP\n";
		$mac = "MACADDR=".$this->m_mac."\n";
		
		fputs($fd,$ip);
		fputs($fd,$gateway);
		fputs($fd,$udpport);
		fputs($fd,$timeout);
		fputs($fd,$protocol);
		fputs($fd, $mac);
		fclose($fd);
		
	}	
}

//$xUdpd = new XUdpd();
//$xUdpd->writeUdpd();
//$ip="192.168.30.1111";
//$mac = $xUdpd->getMac();
//echo $mac;
?>
